<?php

namespace common\models\category;
use yii\base\Model;
use Yii;
use common\models\fenhuicat\Fenhuicat;
use common\models\fenhuigrade\Fenhuigrade;

/**
 * This is the model class for table "category".
 *
 * @property string $id
 * @property string $name
 * @property integer $status
 * @property string $url
 * @property integer $sort
 * @property string $seo_keywords
 * @property string $seo_contents
 * @property integer $parent_id
 */
class Category extends \yii\db\ActiveRecord
{
    const STATUS_INACTIVE = 0;
    const STATUS_ACTIVE = 1;
	private $_statusLabel;
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'category';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['status', 'sort', 'parent_id'], 'integer'],
            [['seo_contents'], 'string'],
            [['name', 'url', 'seo_keywords', 'type', 'ctype'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'name' => Yii::t('app', '栏目名称'),
            'status' => Yii::t('app', '导航显示'),
            'type' => Yii::t('app', '栏目模板'),
            'ctype' => Yii::t('app', '内容模板'),
            'url' => Yii::t('app', '外部链接'),
            'sort' => Yii::t('app', '排序'),
            'seo_keywords' => Yii::t('app', '关键词'),
            'seo_contents' => Yii::t('app', '描述语'),
            'parent_id' => Yii::t('app', '父栏目ID'),
        ];
    }
    /**
     * 栏目分类循环首页
     */
    public static function catlist()
    {
        $plist = Category::findBySql("SELECT * FROM category where status = 1 and parent_id = 0")->asArray()->All();
        foreach ($plist as $key=>$pl){
            $pid=$pl['id'];
            $zlist = Category::findBySql("SELECT * FROM category where status = 1 and parent_id = '$pid'")->asArray()->All();
            $plist[$key]['zlm']=$zlist;
        }
        return $plist;
    }
	
	/**
	* 栏目分类
	*/
	
	
    public static function get($parentId = 0, $array = array(), $level = 0, $add = 2, $repeat = '　')
    {
        $strRepeat = '';
        // add some spaces or symbols for non top level categories
        if ($level>1) {
            for($j = 0; $j < $level; $j ++)
            {
                $strRepeat .= $repeat;
            }
        }

        // i feel this is useless
        if($level>0)
            $strRepeat .= '';

        $newArray = array ();
        $tempArray = array ();

        //performance is not very good here
        foreach ( ( array ) $array as $v )
        {
            if ($v['parent_id'] == $parentId)
            {
                $newArray [] = array ('id' => $v['id'], 'name' => $v['name'], 'parent_id' => $v['parent_id'], 'str_label' => $strRepeat.$v['name'],'status'=>$v['status']);

                $tempArray = self::get ( $v['id'], $array, ($level + $add), $add, $repeat);
                if ($tempArray)
                {
                    $newArray = array_merge ( $newArray, $tempArray );
                }
            }
        }
        return $newArray;
    }
	    public function getStatusLabel()
    {
        if ($this->_statusLabel === null) {
            $statuses = self::getArrayStatus();
            $this->_statusLabel = $statuses[$this->status];
        }
        return $this->_statusLabel;
    }
    /**
     * @inheritdoc
     */
    public static function getArrayStatus()
    {
        return [

            self::STATUS_ACTIVE => Yii::t('app', '是'),
            self::STATUS_INACTIVE => Yii::t('app', '否'),
        ];
    }
	
    public static function isDel($id)
    {
        if (((self::find()->where(['parent_id'=>$id])->one()) != null)) {
			return true;
        } else {
            return false;
        }
    }

	
}
